//
//  ATVInstanceView.m
//  FlashController
//
//  Created by Martin Schiøth Dyrby on 11/15/11.
//  Copyright 2011 Molamil. All rights reserved.
//

#import "ATVInstanceView.h"
#import "ATVInstanceProperty.h"
#import "ATVInstanceNumericProperty.h"
#import "ATVInstanceStringProperty.h"
#import "ATVInstanceBooleanProperty.h"
#import "ATVInstanceObjectProperty.h"
#import "ATVInstanceSelectProperty.h"
#import "ATVNSViewCategory.h"
#import "NSColoredView.h"

@implementation ATVInstanceView

@synthesize javaScriptController;

- (void)awakeFromNib
{
    NSLog(@"Adding instance view as receiver");
        
    [[NSNotificationCenter defaultCenter] 
         addObserver:self 
         selector: @selector(clearWindowHandler:) 
         name:@"clearWindow" 
         object: nil];
}

-(void) clearWindowHandler: (NSNotification *) note {

    NSUInteger subViewCount = [[self subviews] count];
    int intSubViewCount = (int)subViewCount - 1;
    int i;
    for (i = intSubViewCount; i >= 0; i--) {
        NSView* subview = [[self subviews] objectAtIndex:i];
        [subview removeFromSuperview];
        [subview release];
    }
}

- (void) updateInstanceView:(ATVInstance*) instance {
    NSUInteger subViewCount = [[self subviews] count];

    int intSubViewCount = (int)subViewCount - 1;
    int i;
    for (i = intSubViewCount; i >= 0; i--) {
        NSView* subview = [[self subviews] objectAtIndex:i];
        [subview removeFromSuperview];
        [subview release];
    }
    
    int h = 0;

    for (i = instance.items.count - 1; i >= 0; i--) {

        id <ATVInstanceProperty> prop = [instance.items objectAtIndex:i];
        [prop setPath:[[instance.path stringByAppendingString:@"."] stringByAppendingString:prop.name]];
        

        BOOL getCurrentValue = NO;
        
        NSColoredView *view = [prop initView];
        [view setFrameOrigin:NSMakePoint(0,h)];
        NSSize size;
        NSLog(@"path: %@", prop.path);
        if([prop isKindOfClass:[ATVInstanceNumericProperty class]]) {
            getCurrentValue = YES;
            h += 73;
            size = NSMakeSize(self.frame.size.width, 73.0);
        } else if([prop isKindOfClass:[ATVInstanceStringProperty class]]) {
            getCurrentValue = YES;
            h += 126;
            size = NSMakeSize(self.frame.size.width, 126.0);
        } else if([prop isKindOfClass:[ATVInstanceBooleanProperty class]]) {
            getCurrentValue = YES;
            h += 45;
            size = NSMakeSize(self.frame.size.width, 45.0);
        } else if([prop isKindOfClass:[ATVInstanceSelectProperty class]]) {
            getCurrentValue = YES;
            h += 47;
            size = NSMakeSize(self.frame.size.width, 47.0);
        } else  if([prop isKindOfClass:[ATVInstanceObjectProperty class]]) {
            getCurrentValue = NO;
            h += 34;        
            size = NSMakeSize(self.frame.size.width, 34.0);
        }
        [view setFrameSize:size];
        
        if(i%2==0) {
            [view changeColor: [NSColor whiteColor]];
        } else {
            NSColor* theColor = [NSColor colorWithDeviceRed: (float)(240.0/255.0) green: (float)(240.0/255.0) blue: (float)(240.0/255.0) alpha: (float)1.0];
            [view changeColor: theColor];
        }

        [view fixLeftEdge:YES];
        [view fixRightEdge:NO];
        [view fixBottomEdge:YES];
        [view fixTopEdge:YES];
        [self addSubview:view];

        [prop drawView];
        
        if(!prop.isMethod && getCurrentValue) {
            NSDictionary* dict = [[NSDictionary alloc] initWithObjectsAndKeys:[prop path], @"path", nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"requestValue" object:dict];
        }
    }

    NSRect f = self.frame;
    f.size.height = h;
    self.frame = f;
    
    // look at [NSObject performSelector]
}

- (BOOL)isFlipped {
    return YES;
}

@end